package com.ruoyi.recycle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.recycle.domain.HousekeepingCommodity;
import com.ruoyi.recycle.domain.HousekeepingOrder;
import com.ruoyi.recycle.domain.SysTypeConfig;
import com.ruoyi.recycle.dto.HousekeepingOrderQueryDTO;
import com.ruoyi.recycle.mapper.HousekeepingOrderMapper;
import com.ruoyi.recycle.mapper.SysTypeConfigMapper;
import com.ruoyi.recycle.service.IHousekeepingCommodityService;
import com.ruoyi.recycle.service.IHousekeepingOrderService;
import com.ruoyi.recycle.vo.HousekeepingOrderVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 家政订单Service业务层处理
 *
 * @author ruoyi
 * @date 2022-05-22
 */
@Service
public class HousekeepingOrderServiceImpl extends ServiceImpl<HousekeepingOrderMapper, HousekeepingOrder> implements IHousekeepingOrderService {
    @Autowired
    private HousekeepingOrderMapper housekeepingOrderMapper;
    @Autowired
    private IHousekeepingCommodityService commodityService;
    @Resource
    private SysTypeConfigMapper sysTypeConfigMapper;
    /**
     * 查询家政订单
     *
     * @param HKOrderNo 家政订单主键
     * @return 家政订单
     */
    @Override
    public HousekeepingOrder selectHousekeepingOrderById(String HKOrderNo) {
        return housekeepingOrderMapper.selectHousekeepingOrderById(HKOrderNo);
    }

    /**
     * 查询家政订单列表
     *
     * @param housekeepingOrder 家政订单
     * @return 家政订单
     */
    @Override
    public List<HousekeepingOrder> selectHousekeepingOrderList(HousekeepingOrder housekeepingOrder) {
        return housekeepingOrderMapper.selectHousekeepingOrderList(housekeepingOrder);
    }

    /**
     * 新增家政订单
     *
     * @param housekeepingOrder 家政订单
     * @return 结果
     */
    @Override
    public int insertHousekeepingOrder(HousekeepingOrder housekeepingOrder) {
        return housekeepingOrderMapper.insertHousekeepingOrder(housekeepingOrder);
    }

    /**
     * 修改家政订单
     *
     * @param housekeepingOrder 家政订单
     * @return 结果
     */
    @Override
    public int updateHousekeepingOrder(HousekeepingOrder housekeepingOrder) {
        return housekeepingOrderMapper.updateHousekeepingOrder(housekeepingOrder);
    }

    /**
     * 批量删除家政订单
     *
     * @param HKOrderNos 需要删除的家政订单主键
     * @return 结果
     */
    @Override
    public int deleteHousekeepingOrderByIds(String[] HKOrderNos) {
        return housekeepingOrderMapper.deleteHousekeepingOrderByIds(HKOrderNos);
    }

    /**
     * 删除家政订单信息
     *
     * @param id 家政订单主键
     * @return 结果
     */
    @Override
    public int deleteHousekeepingOrderById(Long id) {
        return housekeepingOrderMapper.deleteHousekeepingOrderById(id);
    }

    @Override
    public List<HousekeepingCommodity> selectHousekeepingOrderDetailById(String HKOrderNo) {

        return commodityService.list(Wrappers.lambdaQuery(new HousekeepingCommodity()).in(HousekeepingCommodity::getHousekeepingId,HKOrderNo));
    }

    @Override
    public AjaxResult miniClientGetClientHousekeepingOrder(HousekeepingOrderQueryDTO queryDTO) {

        final HousekeepingOrder order = new HousekeepingOrder();
        BeanUtils.copyProperties(queryDTO, order);
        final LambdaQueryWrapper<HousekeepingOrder> wrapper = Wrappers.lambdaQuery(order);
        if (queryDTO.getSearchTimeBegin() != null) {
            wrapper.ge(HousekeepingOrder::getCreatedTime, queryDTO.getSearchTimeBegin());
        }
        if (queryDTO.getSearchTimeEnd() != null) {
            wrapper.le(HousekeepingOrder::getCreatedTime, queryDTO.getSearchTimeEnd());
        }
        wrapper.orderByDesc(HousekeepingOrder::getCreatedTime);

        IPage<HousekeepingOrder> housekeepingOrderIPage = page(new Page<>(queryDTO.getCurrentPage(), queryDTO.getPageSize(), true), wrapper);

        List<HousekeepingOrder> records = housekeepingOrderIPage.getRecords();

        Page<HousekeepingOrderVO> housekeepingOrderQueryDTOIPage = new Page<>(queryDTO.getCurrentPage(), queryDTO.getPageSize());
        List<HousekeepingOrderVO> housekeepingOrderVOList =  new ArrayList<>();
        for (HousekeepingOrder record : records) {
            HousekeepingOrderVO housekeepingOrderVO = new HousekeepingOrderVO();
            BeanUtils.copyProperties(record,housekeepingOrderVO);
            List<HousekeepingCommodity> housekeepingCommodities = selectHousekeepingOrderDetailById(record.getHKOrderNo());
            if (housekeepingCommodities.size()>0){
                String s = "";
                for (HousekeepingCommodity housekeepingCommodity : housekeepingCommodities) {
                    s = s + housekeepingCommodity.getName() + " ";
                }
                housekeepingOrderVO.setHousekeepingOrderService(s);
                housekeepingOrderVO.setLinkman(record.getCreatedBy() + " " + record.getPhone());
            }
            housekeepingOrderVOList.add(housekeepingOrderVO);
        }
        if (StringUtils.isNotBlank(queryDTO.getType())) {
            housekeepingOrderVOList = housekeepingOrderVOList.stream().filter(housekeepingOrderVO -> housekeepingOrderVO.getHousekeepingOrderService().contains(queryDTO.getType())).collect(Collectors.toList());
        }


        housekeepingOrderQueryDTOIPage.setRecords(housekeepingOrderVOList);

        List<SysTypeConfig> sysTypeConfigList = sysTypeConfigMapper.selectList(new LambdaQueryWrapper<SysTypeConfig>().eq(SysTypeConfig::getType, "家政分类"));

        return AjaxResult.success(housekeepingOrderQueryDTOIPage).put("housekeeping", sysTypeConfigList);
    }

    @Override
    public BigDecimal sumFinishedOrder(LambdaQueryWrapper<HousekeepingOrder> queryWrapper) {
        return getBaseMapper().sumFinishedOrder(queryWrapper);
    }
}
